<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/10
 * Time: 16:05
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Shelves extends Common {
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 上架货物首页
	 * Method Get
	 * @param string get.id 要检索的字段
	 * @return Array res 查询结果
	 * @return string title 检索的字段
	 *
	*/
	public function index()
	{
		$db = Db::table('product')->where('status',7)->order('id desc');
		$where = ['query' =>[]];
		if(!empty(input('get.title'))){
			$title = input('get.title');
			//多条件检索
			$db->where("pname|purchaseprice|retailprice|bid","like","%{$title}%");
			$where['query']['title'] = $title;
		}
		$res = $db->where('type',1)->paginate(7,false,$where);
		return view("index",compact('res','title'));
	}
	/**
	 * 视频预览
	 * Method Get
	 * @param string get.id  商品id
	 * @return Array result 查询结果
	*/
	public function preview()
	{
		if(input('get.id')){
			$result = Db::table('product')->where('id',input('get.id'))->find();
			return view('preview',compact('result'));
		}
	}
	
	/**
	 * 修改商品
	 * @param string get.id 商品id
	 * @param string get.bid 商品添加人id
	*/
	public function edit()
	{
		$id = input('get.id');
		$bid = input('get.bid');
		$title = Db::table('category')->select();
		$linkman = Db::table('business')->select();
		$result = Db::table('product')->where('id',$id)->find();
		//$result['image'] = json_decode($result['image']);
		return view('edit',compact('result','title','linkman','bid'));
	}
	/**
	 * @param string id 商品id
	 * @param string bid 商品添加人id
	 * @param string pname 商品名称
	 * @param string introduce 商品介绍
	 * @param string category 商品类型
	 * @param string purchaseprice 进价
	 * @param string retailprice 售价
	 * @param string cooperation 合作模式
	 * @param string newimage 新图片
	 * @param string newicode 新二维码
	 *
	*/
	public function update()
	{
		try{
			$id = input('post.id');
			$input['pname'] = input('post.pname');
			$bid = input('post.bid');
			$input['introduce'] = input('post.introduce');
			$input['purchaseprice'] = input('post.purchaseprice');
			$input['retailprice'] = input('post.retailprice');
			$input['cooperation'] = input('post.cooperation');
			$input['category'] = input('post.category');
			//$input['customer'] = input('post.customer');
			$input['barcode'] = input('post.barcode');
			if(!empty(input('post.newimage'))){
				$input['image'] = input('post.newimage');
			}
			if(!empty(input('post.newcode'))){
				$input['code'] = input('post.newcode');
			}
			$input['bid'] = $bid;
			Db::startTrans();
			$result = Db::table('product')->where('id',$id)->update($input);
			if(!$result){
				throw new \PDOException('修改失败');
			}
			Db::commit();
			$success = ['success'=>'修改成功'];
			return json($success);
		}catch (\PDOException $e){
			Db::rollback();
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 上传图片
	 * @param array Request->file('image') 图片
	*/
	public function uploadimage()
	{
		if(request()->isAjax()){
			try{
				$bid = input('get.bid');
				if(request()->file('image')){
					$code = request()->file('image');
					$info = $code->validate(['size'=>1567800,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'product' . DS . $bid . DS .'image');
					$codePath = 'product' . DS . $bid . DS .'image';
					if($info){
						$input['image'] = $codePath . DS . $info->getSaveName();
						return json($input);
					}else{
						throw new \PDOException($code->getError());
					}
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	
	/**
	 * 上传二维码
	 * @param array Request->file('code') 二维码
	 */
	public function uploadcode()
	{
		if(request()->isAjax()){
			try{
				$bid = input('get.bid');
				if(request()->file('code')){
					$code = request()->file('code');
					$info = $code->validate(['size'=>1567800,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'product' . DS . $bid . DS .'code');
					$codePath = 'product' . DS . $bid . DS .'code';
					if($info){
						$input['code'] = $codePath . DS . $info->getSaveName();
						return json($input);
					}else{
						throw new \PDOException($code->getError());
					}
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 一件代发
	 * @param string post.id 商品id
	*/
	public function belt()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('product')->where('id',$id)->update(['type'=>2]);
				if(!$result){
					throw new \PDOException('操作失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 复制
	 * @param int post.id 商品id号
	*/
	public function copy()
	{
		try{
			$id = input('post.id');
			if($id < 0){
				throw new \PDOException('请选择有效数据');
			}
			$result = Db::table('product')->where('id',$id)->find();
			unset($result['id']);
			$id = Db::table('product')->insertGetId($result);
			if($id < 0){
				throw new \PDOException('复制失败');
			}
			$success = ['success'=>'复制成功'];
			return json($success);
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 打印条形码
	 * @param string post.id 商品id号
	*/
	public function print_barcode()
	{
		try{
			$info = Db::table('product')->where('id',input('post.id'))->find();
			if(!$info){
				throw new \PDOException('打印失败，信息不存在');
			}
			$barcode = '<C>'.$info['pname'].'</C><BR>';
			$barcode .= '<C>'.$info['retailprice'].'</C><BR>';
			$barcode .= '<C>'.$this->barcode($info['barcode']).'</C><BR>';
			$resultMsg = json_decode(order_print($barcode,1,'617502507'));
			if($resultMsg -> ret != 0){
				throw new \PDOException($resultMsg->msg);
			}
			$success = ['success'=>'打印成功'];
			return json($success);
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 打印条形码
	 * @param string post.id 商品id号
	 */
	public function print_barcode2()
	{
		try{
			$info = Db::table('product')->where('id',input('post.id'))->find();
			if(!$info){
				throw new \PDOException('打印失败，信息不存在');
			}
			$barcode = '<C>'.$info['pname'].'</C><BR>';
			$barcode .= '<C>'.$info['retailprice'].'</C><BR>';
			$barcode .= '<C>'.$this->barcode($info['barcode']).'</C><BR>';
			$resultMsg = json_decode(order_print($barcode,1,'918509828'));
			if($resultMsg -> ret != 0){
				throw new \PDOException($resultMsg->msg);
			}
			$success = ['success'=>'打印成功'];
			return json($success);
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 输入字符串或大写字母生成条形码
	 * 数字或字母结合的总数最多为14位，超出无效
	 * @param string $strnum 需要生成条形码的字符
	 */
	public function barcode($strnum)
	{
		$Letter = array("\x41","\x42","\x43","\x44","\x45","\x46","\x47","\x48","\x49","\x4A","\x4B","\x4C","\x4D","\x4E","\x4F","\x50","\x51","\x52","\x53","\x54","\x55","\x56","\x57","\x58","\x59","\x5A");//匹配大写字母
		$codeB = array("\x30","\x31","\x32","\x33","\x34","\x35","\x36","\x37","\x38","\x39");//匹配字符集B
		$length = strlen($strnum);
		$b=array();
		$b[0] = "\x1b";
		$b[1] = "\x64";
		$b[2] = "\x02";
		$b[3] = "\x1d";
		$b[4] = "\x48";
		$b[5] = "\x32";
		$b[6] = "\x1d";
		$b[7] = "\x68";
		$b[8] = "\x50";
		$b[9] = "\x1d";
		$b[10] = "\x77";
		$b[11] = "\x02";
		$b[12] = "\x1d";
		$b[13] = "\x6b";
		$b[14] = "\x49";
		$b[15]  = chr($length + 2);//长度控制
		$b[16] = "\x7b";
		$b[17] = "\x42";//选择字符集
		for ($i = 0; $i < $length; $i++){
			$temp = substr($strnum,$i,1);
			if(!is_numeric( $temp )){
				switch ($temp) {
					case 'A':
						$temp = '0';
						break;
					case 'B':
						$temp = '1';
						break;
					case 'C':
						$temp = '2';
						break;
					case 'D':
						$temp = '3';
						break;
					case 'E':
						$temp = '4';
						break;
					case 'F':
						$temp = '5';
						break;
					case 'G':
						$temp = '6';
						break;
					case 'H':
						$temp = '7';
						break;
					case 'I':
						$temp = '8';
						break;
					case 'J':
						$temp = '9';
						break;
					case 'K':
						$temp = '10';
						break;
					case 'L':
						$temp = '11';
						break;
					case 'M':
						$temp = '12';
						break;
					case 'N':
						$temp = '13';
						break;
					case 'O':
						$temp = '14';
						break;
					case 'P':
						$temp = '15';
						break;
					case 'Q':
						$temp = '16';
						break;
					case 'R':
						$temp = '17';
						break;
					case 'S':
						$temp = '18';
						break;
					case 'T':
						$temp = '19';
						break;
					case 'U':
						$temp = '20';
						break;
					case 'V':
						$temp = '21';
						break;
					case 'W':
						$temp = '22';
						break;
					case 'X':
						$temp = '23';
						break;
					case 'Y':
						$temp = '24';
						break;
					case 'Z':
						$temp = '25';
						break;
				}
				$iindex = intval($temp);
				$b[18 + $i] = $Letter[$iindex];
			}else{
				$iindex = intval($temp);
				echo $iindex;
				$b[18 + $i] = $codeB[$iindex];
			}
		}
		$str = implode("",$b);
		return $str;
	}
}
